import math
class calculator:
    def __init__(self):
        pass
    def calculate(self,y):
        self.triangle = []
        for i in range(y):
            if i==0:
                self.triangle.append([1])
                continue
            if i==1:
                self.triangle.append([1,1])
                continue
            self.add = [1]
            for j in range(i-1):
                self.add.append(self.triangle[-1][j]+self.triangle[-1][j+1])
            self.add.append(1)
            self.triangle.append(self.add)
        return self.triangle
    def triangle(self,y):
        self.res = []
        for i in range(y):
            n = i+1
            self.res.append(int(n*(n+1)/2))
        return self.res
    def square(self,n,a):
        return n**a
    def factorical(self,x):
        return math.factorial(x)
def inp():
    e = int(input("请输入展开的层数:"))
    cal = calculator()
    result = cal.calculate(e)
    for i in result:
        for j in i:
            print(j,'  ',end='')
        print()
def inp2():
    e = int(input("你要求到第几个三角形数？"))
    cal = calculator()
    result = cal.triangle(e)
    for i in result:
        print(i,'  ',end='')
    print()
def inp3():
    e = int(input("请输入底数:"))
    a = int(input("请输入指数n（-200<n<200）"))
    if not -200<a or not 200>a:
        print("请重新输入！")
        inp3()
    cal = calculator()
    result = cal.square(e,a)
    print(result)
def inp4():
    x = int(input("请输入：几的阶乘？"))
    if x<0 or x%1!=0:
        print("进行阶乘的数必须是正整数哦！")
        inp4()
    cal = calculator()
    result = cal.factorical(x)
    print(result)
if __name__ == '__main__':
    while True:
        try:
            print("规律终结者")
            print("1.杨辉三角")
            print("2.三角形数")
            print("3.求n次方")
            print("4.阶乘")
            res = int(input("请输入："))
            if res == 1:
                inp()
            elif res == 2:
                inp2()
            elif res == 3:
                inp3()
            elif res == 4:
                inp4()

        except:
            print("请输入正确的数字！")